<?php
namespace app\index\controller;
use think\Db;
use think\facade\Cookie;
use think\facade\Session; //使用数据类库

class Index extends \think\Controller
{
    //首页
    public function home()
    {
        //分页
        $blog = Db::name('blog') -> order("blog_time DESC") -> paginate(5);
        $this -> assign("blog",$blog);
        //链接
        $link = Db::name('link') -> order("link_addtime DESC") -> select();
        $this -> assign("link",$link);
        //查询留言
        $guestbook = Db::name('guestbook')
            ->alias('g')
            ->field("g.*,u.user_photo")
            ->join("pre_user u", "g.user_id=u.user_id", "left")
            -> order("gue_addtime DESC") -> paginate(5);
        //处理输出数据
        $guestbook = $guestbook->items();
        foreach ($guestbook as $k => $v) {
            $user_photo = $v['user_id']=='0'?'/static/home/images/100.jpg':'/uploads/'.$v['user_photo'];
            $guestbook[$k]['user_photo'] = $user_photo;

            $gue_content =  $v['gue_content'];
            $gue_content = trim(htmlspecialchars_decode($gue_content,ENT_COMPAT ),'"');
            $guestbook[$k]['gue_content'] = $gue_content;
        }
        $this->assign("guestbook", $guestbook);
        //登录
            $is_login = Session::has('is_login');
            if ($is_login) { //存在 session
            //接收session 值
                $is_session = Session::get('is_login');
                if ($is_session == '1') {
                //session 等于 1
                // 接收cookie
                //判断cookie是否存在
                    $cookie = Cookie::has('user_name');
                    if ($cookie) {
                    //接收cookie 值
                        $user_name = Cookie::get('user_name');
                        $this -> assign('user_name',$user_name,3600);
                    }else {
//                    //删除session
                        Session::delete('is_login');
                        $this -> redirect('/index/index/home'); //重新跳转到登录界面
                    }
                }
            }
           if (!empty($user_name)) {
               $userList = Db::name('user')->where("user_name='$user_name'")->find();
               $user_photo = $v['user_id']=='0'?'/static/home/images/100.jpg':'/uploads/'.$v['user_photo'];
               $userList['user_photo'] = $userList['user_photo'] == ''?'/static/home/images/100.jpg':'/uploads/'.$userList['user_photo'];
               $userList['user_name'] = msubstr( $userList['user_name'],0,8,'utf-8',true);
               $userList['user_content'] = msubstr( $userList['user_content'],0,15,'utf-8',true);
               $this -> assign("userList",$userList);
           }else {
               $user_name = '小粽熊QVQ';
               $userList = Db::name('user')->where("user_name='$user_name'")->find();
               $user_photo = $v['user_id']=='0'?'/static/home/images/100.jpg':'/uploads/'.$v['user_photo'];
               $userList['user_photo'] = $userList['user_photo'] == ''?'/static/home/images/100.jpg':'/uploads/'.$userList['user_photo'];
               $userList['user_name'] = msubstr( $userList['user_name'],0,8,'utf-8',true);
               $userList['user_content'] = msubstr( $userList['user_content'],0,15,'utf-8',true);
               $this -> assign("userList",$userList);
           }

        return $this -> fetch();
    }
    //引导
    public function index()
    {
        return $this -> fetch();
    }
    //文章专栏
    public function article_list()
    {
        //分页
        $blog = Db::name('blog') -> order("blog_time DESC") -> paginate(3);
        $this -> assign("blog",$blog);
        return $this -> fetch();
    }
    //资源分享
    public function resource()
    {
        return $this -> fetch();
    }
    //时间线
    public function little_things()
    {
        return $this -> fetch();
    }
    //关于本站
    public function about()
    {
        //关于作者
        $user= Db::name('user')->where("user_id","1")->find();
        $this -> assign("user",$user);

        //友情链接
        $link = Db::name('link') -> order("link_addtime DESC") -> select();
        $this -> assign("link",$link);
        return $this -> fetch();
    }

    //博客详情页
    public function article($bid)
    {
        //查询当前博客信息
        $blog=Db::name('blog')->where("blog_id=$bid")->find();
        $this->assign('blog',$blog);
        //查询评论
        $reply=Db::name("reply")->where("blog_id=$bid")-> order("reply_time" ,"DESC") -> select();
        $this->assign('reply',$reply);

        //当前页
        $page1=Db::name('blog') -> order("blog_id","ASC") -> limit(1) -> find();
        $next1 = Db::name('blog') -> where("blog_id","$bid")-> limit(1) ->  find();
        //查询上一篇文章
        $page=Db::name('blog')->where("blog_id < $bid")->order("blog_id","DESC") -> limit(1) -> find();
        //查询下一篇文章
        $next=Db::name('blog')->where("blog_id > $bid")->order("blog_id","ASC") -> limit(1) -> find();
        $pageHide = $page1['blog_id'] > $page['blog_id'] ?'opacity: 0':'';
        $pageHide2 = $next['blog_id'] < $next1['blog_id'] ?'opacity: 0':'';

        if ($page['blog_id'] ==''){
            $page['blog_id'] = $page1['blog_id'];
            $page['blog_title'] = $page1['blog_title'];
        }
        if ($next['blog_id'] ==''){
            $next['blog_id'] = $next1['blog_id'];
            $next['blog_title'] = $next1['blog_title'];
        }

        //数据处理输出
        $page['blog_title'] = msubstr($page['blog_title'],0,50,'utf-8',true);
        $next['blog_title'] = msubstr($next['blog_title'],0,50,'utf-8',true);

        //分类导航
        $categories = Db::name('categories')->select();
        $this->assign('categories',$categories);

        $this->assign('pageHide',$pageHide);
        $this->assign('pageHide2',$pageHide2);
        $this->assign('page',$page);
        $this->assign('next',$next);

        $like = Session::has('like');
        if ($like) { //存在 session
            $session = Session::get('like');
            if ($session == '1') {
                $likeStyle = 'like-module_larkLikeBtnActive_2MVy6';
            }
        }else {
            $likeStyle = '';
        }
        $this->assign('likeStyle',$likeStyle);
        return $this -> fetch();
    }
    public function likeBtn(){
        Session::set('like','1');
        $like = Session::has('like');
        if ($like) { //存在 session
            $session = Session::get('like');
            if ($session == '1') {
                $likeStyle = 'like-module_larkLikeBtnActive_2MVy6';
            }else {
                //删除session
                $likeStyle = '';
                Session::delete('like');
            }
        }
        $this->assign('likeStyle',$likeStyle);
    }
    public function delLike(){
        Session::delete('like');
        $likeStyle = '';
        echo $likeStyle;
        $this->assign('likeStyle',$likeStyle);
    }
    //处理评论提交数据
    public function reply(){
        //首先接收表单的数据
        //$data里面是需要验证的数据
        $data=array(
            'reply_email'=>$this->request->post('reply_email'),
            'reply_content'=>$this->request->post('reply_content'),
        );
        //实例化设置好的index/validate/Reply.php的规则
        $validate=new \app\index\validate\Reply1;
        //验证接收到的数据$data
        if(!$validate->check($data)){
            $this->error($validate->getError());//没有通过验证就提示错误
            //$this->redirect('/index/index/index');//不提示直接跳转
        }
        //提示跳转的界面样式\thinkphp\tpl\dispatch_jump.tpl
        $data['reply_time']=time();//评论时间
        //接收隐藏域
        $blog_id=$this->request->post('blog_id');
        $data['blog_id']=$blog_id;
        //执行添加功能
        $res=Db::name('reply')->insert($data);
        //判断结果并跳转页面
        if($res){
            $this->success('评论成功','/index/index/article/bid/'.$blog_id);
        }else{
            $this->error('数据执行有误');
        }
    }

    //处理留言提交数据
    public function guestbook()
    {
        //判断是否登录
        $is_login = Session::has('is_login');
        if ($is_login) { //存在 session
            //接收session 值
            $is_session = Session::get('is_login');
            if ($is_session == '1') {
                // 接收cookie
                //判断cookie是否存在
                $cookie = Cookie::has('user_name');
                if ($cookie) {
                   //接收cookie 值
                    $user_name = Cookie::get('user_name');
                    $this->assign('user_name', $user_name, 3600);
                } else {
                    //删除session
                    Session::delete('is_login');
                    $this->redirect('/index/index/home'); //重新跳转到登录界面
                }
                }
            }
        $user_id= Db::name('user')->where("user_name='$user_name'")->find();
        $user_id = $user_name==$user_id['user_name']?$user_id['user_id']:'0';
        //首先接收表单的数据
        //$data 里面是需要验证的数据
        $data = array(
            'user_id' => $user_id,
            'gue_title' => ' ',
            'gue_email' => $this->request->post('gue_email'),
            'gue_content' => $this->request->post('gue_content'),
        );
        //实例化设置好的 index/validate/Reply.php的规则
        $validate = new \app\index\validate\Reply;
        //验证接收到的数据 $data
        if (!$validate->check($data)) {
            $this->error($validate->getError()); //没用通过验证就提示错误
        }
        //提示跳转的界面样式 \thinkphp\tp1\dispatch_jump.tp1
        $data['gue_addtime'] = time(); //评论时间
        //接收隐藏域
        $gue_id = $this->request->post('gue_id');
        $data['gue_id'] = $gue_id;
        //执行添加功能
        $res = Db::name('guestbook')->insert($data);
        //判断结果并跳转页面
        if ($res) {
            $this->success('留言成功', '/index/index/about');
        } else {
            $this->error('数据执行有误');
        }
    }
}
